//
//  M1SegmengtView.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/13.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1SegmengtView.h"

@interface M1SegmengtView ()

@property (nonatomic, strong) UIButton *m1_letfBtn;

@property (nonatomic, strong) UIButton *m1_rightBtn;

@end

@implementation M1SegmengtView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = m1_color(0xFFFFFF);
        [self m1_createUI];
    }
    return self;
}

- (void)m1_buttonAction:(UIButton *)sender{
    if (sender.tag == 1) {
        [UIView animateWithDuration:0.3 animations:^{
            self.m1_letfBtn.backgroundColor = m1_color(0xFF5A3F);
            [self.m1_letfBtn setTitleColor:m1_color(0xFFFFFF) forState:UIControlStateNormal];
            self.m1_rightBtn.backgroundColor = m1_color(0xFFFFFF);
            [self.m1_rightBtn setTitleColor:m1_color(0xFF5A3F) forState:UIControlStateNormal];
        }];
    }else{
        [UIView animateWithDuration:0.1 animations:^{
            self.m1_letfBtn.backgroundColor = m1_color(0xFFFFFF);
            [self.m1_letfBtn setTitleColor:m1_color(0xFF5A3F) forState:UIControlStateNormal];
            self.m1_rightBtn.backgroundColor = m1_color(0xFF5A3F);
            [self.m1_rightBtn setTitleColor:m1_color(0xFFFFFF) forState:UIControlStateNormal];
        }];
    }
    
    if (self.m1_switchScopeBlock) {
        self.m1_switchScopeBlock(sender.tag);
    }
}

- (void) changeLeftTitle:(NSString *)left right:(NSString *)right{
    [_m1_letfBtn setTitle:left forState:UIControlStateNormal];
    [_m1_rightBtn setTitle:right forState:UIControlStateNormal];
}

- (void)m1_createUI{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = m1_color(0xFFFFFF);
    [self addSubview:bgView];
    
    bgView.layer.cornerRadius = 40 / 2;
    bgView.layer.masksToBounds = YES;
    bgView.layer.borderWidth = 0.5;
    bgView.layer.borderColor = m1_color(0xF0F0F0).CGColor;
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.offset(20);
        make.right.offset(-20);
        make.height.offset(40);
    }];
    
    self.m1_letfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _m1_letfBtn.backgroundColor = m1_color(0xFF5A3F);
    [_m1_letfBtn setTitle:@"个人资料" forState:UIControlStateNormal];
    [_m1_letfBtn setTitleColor:m1_color(0xFFFFFF) forState:UIControlStateNormal];
    _m1_letfBtn.titleLabel.font = m1_font(15);
    _m1_letfBtn.tag = 1;
    [_m1_letfBtn addTarget:self action:@selector(m1_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_m1_letfBtn];
    
    self.m1_rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _m1_rightBtn.backgroundColor = m1_color(0xFFFFFF);
    [_m1_rightBtn setTitle:@"择偶标准" forState:UIControlStateNormal];
    [_m1_rightBtn setTitleColor:m1_color(0xFF5A3F) forState:UIControlStateNormal];
    _m1_rightBtn.titleLabel.font = m1_font(15);
    _m1_rightBtn.tag = 2;
    [_m1_rightBtn addTarget:self action:@selector(m1_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_m1_rightBtn];
    
    NSArray *viewArr = @[self.m1_letfBtn, self.m1_rightBtn];
    
    [viewArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:2 tailSpacing:2];
    [viewArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(2);
        make.bottom.offset(-2);
    }];
    
    _m1_letfBtn.layer.cornerRadius = 18.0;
    _m1_letfBtn.layer.masksToBounds = YES;
    _m1_rightBtn.layer.cornerRadius = 18.0;
    _m1_rightBtn.layer.masksToBounds = YES;
    
}

@end
